/**
 * 本地存储
 */
export function useStore(namespace, version = '1.0.0') {
  localStorage.useStore = true

  let storage = JSON.parse(localStorage[namespace] || '{}')
  let store

  // 版本号匹配
  if (storage.version == version) store = storage.data

  const proxy = new Proxy(store, {
    /**
     * @param {object} target 目标对象
     * @param {string} propKey 被获取的属性名
     * @param {object} receiver Proxy或者继承Proxy的对象
     */
    get(target, propKey, receiver) {
      if (propKey in target) {
        return target[propKey]
      } else {
        throw new ReferenceError(`属性：${propKey} 不存在`)
      }
    },
    set() {},
  })

  console.log('proxy', proxy)

  return proxy

  // return {
  //   store,
  //   saveStore() {
  //     if (!localStorage.useStore) return

  //     let storage = JSON.parse({
  //       version,
  //       data: store,
  //     })

  //     localStorage[namespace] = JSON.stringify(storage)
  //   },
  // }
}
